﻿using Knlght.CH.Domain.IRepository;
using Knlght.Domain;
using Knlght.Model.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Knlght.Service
{
    public class AdministratorsService : ServiceBase
    {
        public AdministratorsService()
        {
        }

        public ActionResponse Add(Administrators model)
        {
            return doAction(() =>
            {
                var rp = getRepository<IAdministratorsRepository>();
                var query = new QueryRequest<Administrators>();
                query.Add(o => o.Username == model.Username);
                if (rp.IsExist(query))
                {
                    throw new EJApplicationException("用户名已存在！");
                }
                model.Id = Guid.NewGuid().ToString();
                model.IsSuper = false;
                model.Password = Encipher("123456");
                return rp.Add(model);
            });
        }
        public ActionResponse Add(List<Administrators> models)
        {
            return doAction(() =>
            {
                var rp = getRepository<IAdministratorsRepository>();
                return rp.AddToList(models);
            });
        }
        public ActionResponse Update(Administrators model)
        {
            return doAction(() =>
            {
                var rp = getRepository<IAdministratorsRepository>();
                var query = new QueryRequest<Administrators>();
                query.Add(o => o.Username == model.Username);
                query.Add(o => o.Id != model.Id);
                if (rp.IsExist(query))
                {
                    throw new EJApplicationException("用户名已存在！");
                }
                model.Password = Encipher(model.Password);
                return rp.Update(model);
            });
        }
        public ActionResponse Update(QueryRequest<Administrators> request, Expression<Func<Administrators, Administrators>> key)
        {
            return doAction(() =>
            {
                var rp = getRepository<IAdministratorsRepository>();
                return rp.UpdateToList(request, key);
            });
        }
        public ActionResponse Remove(QueryRequest<Administrators> request)
        {
            return doAction(() =>
            {
                var rp = getRepository<IAdministratorsRepository>();
                return rp.Delete(request);
            });
        }

        public PagingResponse<Administrators> Paging(PagingRequest<Administrators> request)
        {
            return GetPaging<Administrators>(() =>
            {
                //总记录
                int totalRecord = 0;
                var rp = getRepository<IAdministratorsRepository>();              
                return new PagingList<Administrators>()
                {
                    Models = rp.GetList(request.PageSize, request.PageIndex, out totalRecord, request.Query),
                    TotalModels = totalRecord,
                    TotalPages = (totalRecord + request.PageSize - 1) / request.PageSize
                };
            });
        }

        public ItemResponse<Administrators> CheckLogin(string username, string password)
        {
            var rp = getRepository<IAdministratorsRepository>();
            var query = new QueryRequest<Administrators>();
            query.Add(o => o.Username == username);
            var user = rp.GetModel(query);
            if (user != null)
            {
                if (user.IsEnabled)
                {
                    var dbPassword = Encipher(password);
                    if (dbPassword == user.Password)
                    {

                        return GetModel(() =>
                        {
                            return user;
                        });
                    }
                    else
                    {
                        return new ItemResponse<Administrators>(false, "密码不正确，请重新输入");
                    }
                }
                else
                {
                    return new ItemResponse<Administrators>(false, "账户被系统锁定, 请联系管理员");
                }
            }
            return new ItemResponse<Administrators>(false, "账户不存在");
        }
        public ItemResponse<Administrators> GetModel(string id)
        {
            return GetModel(() =>
            {
                var rp = getRepository<IAdministratorsRepository>();
                var query = new QueryRequest<Administrators>();
                query.Add(o => o.Id == id);
                return rp.GetModel(query);
            });
        }
    }
}
